package cn.rocky.pandacodegen.controller;

import cn.rocky.pandacodegen.common.R;
import cn.rocky.pandacodegen.common.config.GeneratorConfig;
import cn.rocky.pandacodegen.common.param.GeneratorParam;
import cn.rocky.pandacodegen.domain.entity.DatasourceConfig;
import cn.rocky.pandacodegen.domain.vo.CodeFile;
import cn.rocky.pandacodegen.service.DatasourceConfigService;
import cn.rocky.pandacodegen.service.GeneratorService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("generate")
public class GeneratorController {

    private final DatasourceConfigService datasourceConfigService;

    private final GeneratorService generatorService;

    /**
     * 生成代码
     *
     * @param generatorParam 生成参数
     * @return 返回代码内容
     */
    @RequestMapping("/code")
    public R<List<CodeFile>> code(@RequestBody GeneratorParam generatorParam) {
        int datasourceConfigId = generatorParam.getDatasourceConfigId();
        DatasourceConfig datasourceConfig = datasourceConfigService.getById(datasourceConfigId);
        GeneratorConfig generatorConfig = GeneratorConfig.build(datasourceConfig);
        return new R().ok(R.CODE_VUE,generatorService.generate(generatorParam, generatorConfig));
    }
}
